package com.biz.primus.ms.promotion.trans;

import com.biz.primus.model.promotion.vo.SpecialOfferProductVo;
import com.biz.primus.model.promotion.vo.SpecialOfferPromotionVo;
import com.biz.primus.ms.promotion.dao.po.SpecialOfferPromotion;
import com.google.common.collect.Lists;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import org.apache.commons.lang.StringUtils;

/**
 * @author david-liu
 */
public class SpecialOfferPromotionPo2VoMapper implements Function<SpecialOfferPromotion, SpecialOfferPromotionVo> {
    @Override
    public SpecialOfferPromotionVo apply(SpecialOfferPromotion specialOfferPromotion) {
        SpecialOfferPromotionVo vo = new SpecialOfferPromotionVo();
        vo.setId(specialOfferPromotion.getId());
        vo.setName(specialOfferPromotion.getName());
        vo.setDescription(specialOfferPromotion.getDescription());
        vo.setChannels(specialOfferPromotion.getChannels());
        vo.setScope(specialOfferPromotion.getScope());
        vo.setScopeIds(StringUtils.join(specialOfferPromotion.getScopeIds(), ","));
        vo.setEffectMemberLevels(specialOfferPromotion.getEffectMemberLevelStringList());
        if (Objects.nonNull(specialOfferPromotion.getStartTime())) {
            vo.setStartTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(specialOfferPromotion.getStartTime()));
        }
        if (Objects.nonNull(specialOfferPromotion.getEndTime())) {
            vo.setEndTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(specialOfferPromotion.getEndTime()));
        }
        vo.setCommonStatus(specialOfferPromotion.getCommonStatus());
        vo.setCreateTimestamp(specialOfferPromotion.getCreateTimestamp());
        return vo;
    }

    public List<SpecialOfferPromotionVo> mapWithList(List<SpecialOfferPromotion> specialOfferPromotions, Map<Long, List<SpecialOfferProductVo>> specialOfferProductVoMap) {
        List<SpecialOfferPromotionVo> ret = Lists.newArrayList();
        for (SpecialOfferPromotion specialOfferPromotion : specialOfferPromotions) {
            SpecialOfferPromotionVo specialOfferPromotionVo = this.apply(specialOfferPromotion);
            specialOfferPromotionVo.setProducts(specialOfferProductVoMap.getOrDefault(specialOfferPromotion.getId(), Lists.newArrayList()));
            ret.add(specialOfferPromotionVo);
        }
        return ret;
    }

    public SpecialOfferPromotionVo map(SpecialOfferPromotion specialOfferPromotion, List<SpecialOfferProductVo> specialOfferProductVos) {
        SpecialOfferPromotionVo specialOfferPromotionVo = this.apply(specialOfferPromotion);
        specialOfferPromotionVo.setProducts(specialOfferProductVos);
        return specialOfferPromotionVo;
    }
}
